package com.zs.zcerp.approval.controller;

import com.zs.zcerp.approval.entity.EmpEntity;
import com.zs.zcerp.approval.entity.HtmlVO;
import com.zs.zcerp.approval.service.IMailService;
import com.zs.zcerp.approval.service.IQhChkmgrdService;
import com.zs.zcerp.common.controller.BaseController;
import com.zs.zcerp.entrance.init.InitEntranceData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.text.SimpleDateFormat;
import java.util.Base64;
import java.util.Date;
import java.util.List;

/**
 * 定时任务,签核第一个步骤发送邮件
 */
@Configuration
@EnableScheduling
public class MailScheduled extends BaseController {

    private final static Logger logger = LoggerFactory.getLogger(InitEntranceData.class);

    final static SimpleDateFormat df= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Autowired
    private  IQhChkmgrdService iQhChkmgrdService;

    @Autowired
    private   IMailService mailService;

    public MailScheduled() {
    }


    @Scheduled(cron = "0 0 23 * * ?")   //每天23点执行一次
    public void sendSginMail() {

        logger.info(df.format(new Date()) + "开始定时更新签核数据...");

        // 定时更新公司信息
        try {
            List<EmpEntity> list = iQhChkmgrdService.querySignYesterDay();

            logger.info("待更新公司数量:" + list.size());
            for (int i = 0; i < list.size(); i++) {
                HtmlVO htmlVO = mailHtml(list.get(i));
                mailService.sendHtmlMail(list.get(i).getToMail(), htmlVO.getSubject(), htmlVO.getContent());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        logger.info(df.format(new Date()) + "定时更新签核数据完毕");
    }


    public HtmlVO mailHtml(EmpEntity e) {
        HtmlVO htmlVO = new HtmlVO();
        htmlVO.setToEmail(e.getToMail());
        htmlVO.setContent(
                "<!DOCTYPE html>" +
                        "<html lang='en'>" +
                        " <head></head> \n" +
                        " <body> " +
                        "  <font size='4' color='black'></font> \n" +
                        "  <p align='left'><font>各位同仁:<br />&nbsp;&nbsp;大家好！<br />&nbsp;&nbsp;以下销毁单据已送签至各位，请及时完成单据审核:</font> </p> \n" +
                        "  <font size='4' face='标楷体' color='black'></font> \n" +
                        "  <table border='1' cellspacing='0' cellspace='0'> \n" +
                        "   <tbody> \n" +
                        "    <tr align='left'> \n" +
                        "     <td>组织</td> \n" +
                        "     <td>销毁单号</td> \n" +
                        "     <td>送签人</td> \n" +
                        "     <td>送签时间</td> \n" +
                        "     <td>上级签核人</td> \n" +
                        "     <td>上级签核时间</td> \n" +
                        "    </tr> \n" +
                        "    <tr align='center'> \n" +
                        "     <td nowrap=''>" + e.getOrgName() + "</td> \n" +
                        "     <td nowrap=''>" + e.getOrderNo() + "</td> \n" +
                        "     <td nowrap=''>" + e.getGrtUserCname() + "</td> \n" +
                        "     <td nowrap=''>" + e.getSendDate() + "</td> \n" +
                        "     <td nowrap=''>" +  "</td> \n" +
                        "     <td nowrap=''>" +   "</td> \n" +
                        "    </tr> \n" +
                        "   </tbody>" +
                        "</table> \n" +
                        "  <bottom> \n" +
                        "   <font size='4' color='black'><u></u><p align='left'><u>签核网址:<a href='http://192.168.33.28:8000/'>http://192.168.33.28:8000/</a> <br /></u><font size='4' color='black'><br />Best Regards<br />总公司品牌保护-何三华<br />TEL:674686<br />2021/05/15</font></p></font> \n" +
                        "  </bottom>  \n" +
                        " </body>\n" +
                        "</html>"
        );
        htmlVO.setSubject("单据签核");
        return htmlVO;
    }

}
